package com.project8.main;

import android.app.Activity;
import android.content.ContentResolver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;

public class BrightnessSettings 
{	
	public BrightnessSettings(Activity main, SeekBar brightbar, final Window window, final ContentResolver contRes)
	{
		try
		{
			float initialBrightness = android.provider.Settings.System.getInt(main.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
			float percentInitialBrightness = initialBrightness/255;
			
			if(percentInitialBrightness == 0.0)
				percentInitialBrightness += 0.05f;
			
			WindowManager.LayoutParams layoutParams = window.getAttributes();
			layoutParams.screenBrightness = percentInitialBrightness;
			window.setAttributes(layoutParams);
			
			brightbar.setProgress((int)initialBrightness);
				
			android.provider.Settings.System.putInt(contRes, android.provider.Settings.System.SCREEN_BRIGHTNESS, (int)initialBrightness);
			
			brightbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
			{
			   public void onProgressChanged(final SeekBar arg0, int arg1, boolean arg2) 
			   {
				   float backLightValue = (float)arg1;
				   int sysBackLightValue = (int)backLightValue;
				   float percentFullBrightness = backLightValue/255;
				   
				   if(percentFullBrightness == 0.0)
					   percentFullBrightness += 0.05f;
				   
				   WindowManager.LayoutParams layoutParams = window.getAttributes();
				   layoutParams.screenBrightness = percentFullBrightness;
				   window.setAttributes(layoutParams);
				   
				   System.out.println("BackLightVal = " + backLightValue);
				   System.out.println("SysBackLightVal = " + sysBackLightValue);
				   System.out.println("Percent Brightness = " + percentFullBrightness);
			     
			       android.provider.Settings.System.putInt(contRes, android.provider.Settings.System.SCREEN_BRIGHTNESS, sysBackLightValue);		       
			   }
	
			   public void onStartTrackingTouch(SeekBar arg0){}
	
			   public void onStopTrackingTouch(SeekBar arg0){}
			});
		}
		catch(Exception e){ e.printStackTrace(); }
	}

}
